﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TL_Cloud.Core.Entities;
using TL_Cloud.Core.Services;
using TL_Cloud.Models.BookShop;
using TL_Cloud.RepositoryInterface.BookShop;
using TL_Cloud.ViewModels.BookShop.BaseTeachClass;

namespace TL_Cloud.RepositoryService.BookShop
{
    /// <summary>
    /// 教学班级的仓存
    /// </summary>
    public class BaseTeachClassRepository : BaseRepository<Base_TeachClass>, IBaseTeachClassRepository
    {
        public BaseTeachClassRepository(AppBookShopContext myContext,
     IPropertyMappingContainer propertyMappingContainer) : base(myContext, propertyMappingContainer)
        {
            currentDbSet = _myContext.BaseTeachClass;
        }

        public async Task<PaginatedList<Base_TeachClass>> GetAllEntitiesAsync(BaseTeachClassQueryParameters parameters)
        {
            var query = currentDbSet.AsQueryable();
            if(string.IsNullOrEmpty(parameters.teacherId)==false)
            {
                query = query.Where(n=>n.TeacherId== parameters.teacherId);
            }
            int count = 0;
            List<Base_TeachClass> data = new List<Base_TeachClass>();
            if (parameters.isPage)
            {
                count = await query.CountAsync();
                data = await query
                    .Skip(parameters.PageIndex * parameters.PageSize)
                    .Take(parameters.PageSize)
                    .ToListAsync();
            }
            else
            {
                data = await query.ToListAsync();
            }
            return new PaginatedList<Base_TeachClass>(parameters.PageIndex, parameters.PageSize, count, data);
        }
    }
}
